package com.touzhijia.scheduler.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.touzhijia.common.persistence.Page;
import com.touzhijia.scheduler.entity.QrtzSchedulerLog;
import com.touzhijia.scheduler.mapper.QrtzSchedulerLogMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 作者：lizw <br/>
 * 创建时间：2017/6/1 11:31 <br/>
 */
@Service
@Transactional(readOnly = true)
public class SchedulerLogService {
    /**
     * 日志对象
     */
    private final static Logger logger = LoggerFactory.getLogger(SchedulerLogService.class);

    @Autowired
    private QrtzSchedulerLogMapper qrtzSchedulerLogMapper;

    @Transactional(readOnly = false)
    public QrtzSchedulerLog saveQrtzSchedulerLog(QrtzSchedulerLog qrtzSchedulerLog) {
        qrtzSchedulerLogMapper.insertSelective(qrtzSchedulerLog);
        return qrtzSchedulerLog;
    }

    /**
     * 分页查询调度器日志
     *
     * @param page          分页数据
     * @param schedulerName 调度器名称
     * @param instanceName  调度器ID
     * @param methodName    触发事件调用的方法
     * @param logTimeStart  记录时间-起始值
     * @param logTimeEnd    记录时间-结束值
     * @return 触发器日志分页数据
     */
    public PageInfo<QrtzSchedulerLog> findByPage(Page<QrtzSchedulerLog> page, String schedulerName, String instanceName, String methodName, Date logTimeStart, Date logTimeEnd) {
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        List<QrtzSchedulerLog> list = qrtzSchedulerLogMapper.findByPage(schedulerName, instanceName, methodName, logTimeStart, logTimeEnd);
        return new PageInfo<>(list);
    }
}
